/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecordPanel.java
 *
 * Created on 25-lug-2009, 1.34.09
 */
package tr3dtris.game.display.ui;

import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import tr3dtris.game.persistence.Record;
import tr3dtris.game.persistence.RecordArray;

/**
 *
 * @author Luciano
 */
public class RecordPanel extends javax.swing.JPanel {

    RecordArray records;

    /** Creates new form RecordPanel */
    public RecordPanel(RecordArray records) {
        this.records = records;
        initComponents();
        refreshRecordList();
    }

    public void refreshRecordList() {
        DefaultListModel listModel = new DefaultListModel();
        this.records = RecordArray.load();
        Vector<Record> r = this.records.getRecords();
        for (int i = 0; i < r.size(); i++) {
            listModel.addElement(r.get(i));
        }
        this.listRecords.setModel(listModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listRecords = new javax.swing.JList();
        buttonClear = new javax.swing.JButton();

        jScrollPane1.setViewportView(listRecords);

        buttonClear.setText("Clear records");
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, buttonClear))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(buttonClear)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
        if (JOptionPane.showConfirmDialog(this,
                "This will remove all your stored records. Would you like to continue?", 
                "Clear records", 
                JOptionPane.WARNING_MESSAGE, 
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) 
        {
            this.records.clear();
            this.refreshRecordList();
        }
    }//GEN-LAST:event_buttonClearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClear;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listRecords;
    // End of variables declaration//GEN-END:variables
}
